﻿using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RotovapPlatform.Infrastructure.Common.Data
{
	/// <summary>
	/// 实验组数据库上下文工厂
	/// 用于EF Core迁移时创建DbContext实例
	/// </summary>
	public class DesignTimeExperimentGroupDbContextFactory : IDesignTimeDbContextFactory<ExperimentGroupDbContext>
	{
		public ExperimentGroupDbContext CreateDbContext(string[] args)
		{
			var optionsBuilder = new DbContextOptionsBuilder<ExperimentGroupDbContext>();
			var solutionDir = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..\\..\\.."));
			var infrastructureDir = Path.Combine(solutionDir, "src", "RotovapPlatform.Infrastructure");
			var folder = Path.Combine(infrastructureDir, "Data");

			Console.WriteLine($"Looking for database in: {folder}");
			if (!Directory.Exists(folder))
				Directory.CreateDirectory(folder);

			var dbPath = Path.Combine(folder, "Rotovap.db");
			Console.WriteLine($"Database file should be at: {dbPath}");
			optionsBuilder.UseSqlite($"Data Source={dbPath}");

			return new ExperimentGroupDbContext(optionsBuilder.Options);
		}
	}
}
